package com.xh.demo.config.custom;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.xh.demo.common.enums.ErrorEnum;
import com.xh.demo.common.exception.BusinessException;
import com.xh.demo.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Name CustomJsonDateDeserializer
 * @Description JSON反序列化日期处理器
 * 1.配合@JsonDeserialize(using = CustomJsonDateDeserializer.class)使用(实体属性注解),
 * 或者全局配置(可处理ajax请求的json数据中的日期字符串)
 * 2.但不能处理form表单提交的日期字符串
 * @Author wen
 * @Date 2019-08-02
 */
public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {

    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        return DateUtil.parseDate(jp.getText());
    }

    @Override
    public Class<?> handledType() {
        return Date.class;
    }

}
